using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DungeonGenerator.WinGUI
{
    public partial class Form1 : Form
    {
        private Dungeon dungeon;
        private const int xStep = 20;
        private const int yStep = 20;
        private const int xOffset = 20;
        private const int yOffset = 20;
        private readonly DungeonGenerator generator = new DungeonGenerator(25, 25, 30, 70, 80, new RoomGenerator(10, 1, 6, 1, 6));

        public Form1()
        {
            InitializeComponent();
            
            dungeon = generator.Generate();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Font font = new Font("Arial", 8);

            foreach (Point location in dungeon.CellLocations)
            {
                Cell cell = dungeon[location];

                g.FillRectangle(GetWallCount(cell) == 4 ? Brushes.Black : Brushes.Transparent, TopLeft(location).X, TopLeft(location).Y, xStep, yStep);

                Pen pen = GetSidePen(cell.NorthSide);
                g.DrawLine(pen, TopLeft(location), TopRight(location));
                if (cell.NorthSide == SideType.Door) g.DrawRectangle(pen, TopLeft(location).X + 1 + (xStep - 2) / 4, TopLeft(location).Y - 3, (xStep / 2) - 1, 6);

                pen = GetSidePen(cell.WestSide);
                g.DrawLine(pen, TopLeft(location), BottomLeft(location));
                if (cell.WestSide == SideType.Door) g.DrawRectangle(pen, TopLeft(location).X - 3, TopLeft(location).Y + 1 + (yStep - 2) / 4, 6, (xStep / 2) - 1);

                g.DrawLine(GetSidePen(cell.SouthSide), BottomLeft(location), BottomRight(location));
                g.DrawLine(GetSidePen(cell.EastSide), TopRight(location), BottomRight(location));
            }
        }

        private static int GetWallCount(Cell cell)
        {
            int wallCount = 0;
            if (cell.NorthSide == SideType.Wall) wallCount++;
            if (cell.SouthSide == SideType.Wall) wallCount++;
            if (cell.WestSide == SideType.Wall) wallCount++;
            if (cell.EastSide == SideType.Wall) wallCount++;
            return wallCount;
        }

        private static Point TopLeft(Point location)
        {
            return new Point(xOffset + (location.X * xStep), yOffset + (location.Y * yStep));
        }

        private static Point BottomLeft(Point location)
        {
            return new Point(xOffset + (location.X * xStep), yOffset + (location.Y * yStep) + yStep);
        }

        private static Point TopRight(Point location)
        {
            return new Point(xOffset + (location.X * xStep) + xStep, yOffset + (location.Y * yStep));
        }

        private static Point BottomRight(Point location)
        {
            return new Point(xOffset + (location.X * xStep) + xStep, yOffset + (location.Y * yStep) + yStep);
        }

        private static Pen GetSidePen(SideType SideType)
        {
            switch (SideType)
            {
                case SideType.Wall:
                case SideType.Door:
                    return new Pen(Color.Black);
                case SideType.Empty:
                    return new Pen(Color.Transparent);
                default:
                    return new Pen(Color.Red);
            }
        }

        private void Form1_Click(object sender, EventArgs e)
        {
            dungeon = generator.Generate();
            Invalidate();
        }
    }
}